#!/usr/bin/env python3
from fire import Fire
import gzip
from collections import defaultdict
from os.path import dirname
from datetime import datetime
import zd 
import sys

def extract(egin, end, html):
    if not html:
        return
    if egin is None:
        start = 0
    else:
        start = html.find(egin)
        if start >= 0:
            start += len(egin)+1
        else:
            return
    if end is not None:
        end = html.find(end, start)
    if end is None or end >= 0:
        return html[start:end].strip()



@Fire
def main(infile):
    outfile = infile[:-3]+".csv.zst"

    exist = defaultdict(int)
    for i in gzip.open(infile):
        time = int(extract(b'sessionTimestamp":',b'"',i) or 0)//1000
        AndroidId = extract(b'AndroidId":',b'"',i)
        if time and AndroidId:
            if exist[AndroidId] > time:
                continue
            exist[AndroidId] = time

    with zd.open(outfile,"w") as out:
        for k,v in sorted(exist.items(),key=lambda x:x[0]):
            out.write(k+b","+str(datetime.fromtimestamp(v)).encode('utf8')+b"\n")

